package com.zbkj.service.service.order.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zbkj.dal.base.enums.ShareOrderLogTypeEnums;
import com.zbkj.dal.common.exception.CrmebException;
import com.zbkj.dal.dao.ShareOrderOperateLogMapper;
import com.zbkj.dal.entity.ba.EbSupplyOrderOperateLogEntity;
import com.zbkj.dal.pojo.response.UserInfoResponse;
import com.zbkj.service.model.front.req.log.ShareOrderLogCreateReq;
import com.zbkj.service.model.front.vo.log.ShareOrderLogVO;
import com.zbkj.service.service.UserService;
import com.zbkj.service.service.order.ShareOrderOperateLogService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionTemplate;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author wangzhengyan
 * @date 2024/4/6 2:48 PM
 */
@Service
@Slf4j
public class ShareOrderOperateLogServiceImpl extends ServiceImpl<ShareOrderOperateLogMapper, EbSupplyOrderOperateLogEntity> implements ShareOrderOperateLogService {

    @Resource
    private TransactionTemplate transactionTemplate;
    @Resource
    private UserService userService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addLog(ShareOrderLogCreateReq req) {
        if (Objects.isNull(req)) {
            throw new CrmebException("添加日志参数不能为空");
        }
        // todo 需确认是否对修改内容进行字数限制
        if (req.getOperateContent().length() > 200) {
            throw new CrmebException("修改内容不能超过200字");
        }
        UserInfoResponse userInfo = userService.getUserInfo();
        EbSupplyOrderOperateLogEntity entity = new EbSupplyOrderOperateLogEntity();
        entity.setOrderNo(req.getOrderNo());
        entity.setOperateType(req.getOperateType());
        entity.setOperateId(userInfo.getId());
        entity.setOperator(userInfo.getNickname());
        entity.setCreatedAt(LocalDateTime.now());
        entity.setCreatedBy(userInfo.getId());
        entity.setContent(req.getOperateContent());

        return transactionTemplate.execute((e) -> {
            save(entity);
            return Boolean.TRUE;
        });
    }

    @Override
    public List<ShareOrderLogVO> getLogList(String orderNo) {
        if (StringUtils.isBlank(orderNo)) {
            return Collections.emptyList();
        }
        LambdaQueryWrapper<EbSupplyOrderOperateLogEntity> queryWrapper = Wrappers.lambdaQuery(EbSupplyOrderOperateLogEntity.class);
        queryWrapper.eq(EbSupplyOrderOperateLogEntity::getOrderNo, orderNo);
        queryWrapper.orderByDesc(EbSupplyOrderOperateLogEntity::getId);
        List<EbSupplyOrderOperateLogEntity> pageList = list(queryWrapper);
        if (CollectionUtils.isEmpty(pageList)) {
            return Collections.emptyList();
        }
        // 构建返参
        return pageList.stream().map(ShareOrderLogVO::build).collect(Collectors.toList());
    }

    @Override
    public ShareOrderLogVO getOneLog(String orderNo, ShareOrderLogTypeEnums operateType) {
        if (StringUtils.isBlank(orderNo)) {
            return null;
        }
        LambdaQueryWrapper<EbSupplyOrderOperateLogEntity> queryWrapper = Wrappers.lambdaQuery(EbSupplyOrderOperateLogEntity.class);
        queryWrapper.eq(EbSupplyOrderOperateLogEntity::getOrderNo, orderNo);
        queryWrapper.eq(EbSupplyOrderOperateLogEntity::getOperateType, operateType.getName());
        EbSupplyOrderOperateLogEntity operateLogEntity = getOne(queryWrapper);
        // 构建返参
        return ShareOrderLogVO.build(operateLogEntity);
    }

}
